﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WoChu.RfClient.Entity;
using WoChu.RfClient.Duty;
using WoChu.RfClient.Common;
using WoChu.CF.BaseFrame.Util;

namespace WoChu.RfClient.Duty
{
    public partial class FrmOffShelves : Form
    {
        public static GoodsBoxes goodsBox = new GoodsBoxes();
        public FrmOffShelves()
        {
            InitializeComponent();
        }
        //public FrmOffShelves(string bCode)
        //{
        //    _boxCode = bCode;
        //    InitializeComponent();
        //}
        private static Dictionary<string, LocationGoodsInfo> _dictLocationGoods = null;
        public static Dictionary<string, LocationGoodsInfo> dictLocationGoods
        {
            get
            {
                if (_dictLocationGoods == null) _dictLocationGoods = new Dictionary<string, LocationGoodsInfo>();
                return _dictLocationGoods;
            }
        }

        private static Dictionary<string, LocationGoodsInfo> _dictLocation = null;
        public static Dictionary<string, LocationGoodsInfo> dictLocation
        {
            get
            {
                if (_dictLocation == null) _dictLocation = new Dictionary<string, LocationGoodsInfo>();
                return _dictLocation;
            }
        }

        private static List<LocationGoodsInfo> _lstLocationGoods = null;
        public static List<LocationGoodsInfo> lstLocationGoods
        {
            get
            {
                if (_lstLocationGoods == null) _lstLocationGoods = new List<LocationGoodsInfo>();
                return _lstLocationGoods;
            }
            set
            {
                _lstLocationGoods = value;
            }
        }

        private static int iSort = 0;
        private static List<string>  arrayBarCode = new List<string>();//added by tao 2015-11-10
        private void FrmOffShelves_Load(object sender, EventArgs e)
        {
            txtGoodsBatchCode.Focus(); 
        }
        //提交
        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                //if (WoChu.CF.BaseFrame.Util.MessageBoxEx.Question("是否绑定箱号？") == DialogResult.Yes)
                //{
                //    if (goodsBox == null || string.IsNullOrEmpty(goodsBox.GOODSBOXCODE))
                //    {
                //        //this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
                //        FrmBindBoxCode fbbc = new FrmBindBoxCode(false);
                //        //fbbc.StartPosition = FormStartPosition.CenterParent;
                //        fbbc.ShowDialog();
                //    }
                //if (WoChu.CF.BaseFrame.Util.MessageBoxEx.Question("是否提交？绑定箱号：." + goodsBox.GOODSBOXCODE + ",波次：" + goodsBox.BATCHNO + ",配送时间：" + goodsBox.RATIONDATE + " , 修改箱号等信息请退出下架界面！") == DialogResult.No) return;

                //}
                //for (int i = 0; i < lstLocationGoods.Count; i++)
                //{
                //    if (lstLocationGoods[0].QTY == 0)
                //        lstLocationGoods.Remove(lstLocationGoods[0]);
                //}
                lstLocationGoods = lstLocationGoods.FindAll(x => x.QTY > 0);
                if (lstLocationGoods != null && lstLocationGoods.Count() > 0)
                { 
                    List<LocationGoodsInfo> lgitems = lstLocationGoods.FindAll(x => x.QTY > x.INVQTY);
                    if (lgitems.Count>0)
                    {
                        //MessageBoxEx.Show(string.Format("商品批次号：{0}，\n的下架数量大于库存,请修改下架数量！", lgitem.GOODSBATCHCODE));
                        MessageBoxEx.Question(string.Format("有{0}条数据的下架数量大于库存，是否继续提交？", lgitems.Count));
                        
                            //Sort(lgitem);
                            ////设置已扫描的为加亮
                            //SelectionBright();
                            return;
                        
                    }
 
                    //DateTime rationdate=goodsBox.RATIONDATE.HasValue?goodsBox.RATIONDATE.Value:DateTime.Parse("1900-01-01");
                    //DateTime rationdate = DateTime.Parse("1900-01-01");
                    //goodsBox.GOODSBOXCODE = "";
                    OutPutParams<bool> re = Duty.OffShelvesUIRule.NewInstance.SetOffShelves(LoginBean.Instance.USERCODE,lstLocationGoods);
  
                    if (re.Params)
                    {
                        lstLocationGoods.Clear();
                        this.OffShelvesSource.DataSource = null;
                        this.dtGridOffShelves.DataSource = this.OffShelvesSource;
 
 
                        //txtGoodsBatchCode.Text = string.Empty;  
                        //txtLocation.Text = string.Empty; 
                        //txtQty.Text = string.Empty;
                        //txtLocation.Focus(); 

                        WoChu.CF.BaseFrame.Util.MessageBoxEx.Show("下架提交成功！");

                        if (arrayBarCode.Count > 0)
                        {
                            foreach (var item in arrayBarCode)
                            {
                                LocationGoodsInfo _LocationGoods = Duty.OffShelvesUIRule.NewInstance.GetLocationGoodsInfo(item);
                                if (_LocationGoods != null)
                                    arrayBarCode.Add(item);
                            }
                        }
                        this.OffShelvesSource.DataSource = lstLocationGoods;
                        this.dtGridOffShelves.DataSource = this.OffShelvesSource;
                        this.dtGridOffShelves.Refresh();
                            
               

                    }
                    else
                    {
                        MessageBox.Show("提交失败！\n" + re.Message);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }
        //回车商品批次号
        private void txtGoodsBatchCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;

            try
            {
                string gbCode = txtGoodsBatchCode.Text.Trim();
                string LtCode = txtLocation.Text.Trim();
                if (string.IsNullOrEmpty(gbCode))
                {
                    MessageBox.Show("请扫描商品批次条码！");
                    txtGoodsBatchCode.SelectAll();
                    return;
                }
                if (gbCode.Length != 18 && gbCode.Length != 17 && gbCode.Length != 20)
                {
                    MessageBox.Show("请扫描正确的商品批次条码！");
                    txtGoodsBatchCode.SelectAll();
                    return;
                }
 
                int curIndex = lstLocationGoods.FindIndex(x => x.GOODSBATCHCODE == gbCode);
                if (curIndex>=0)
                {
                    //LocationGoodsInfo _LocationGoods = dictLocationGoods[barCode];
                    //int curIndex = lstLocationGoods.FindIndex(x => x.GOODSBATCHCODE == gbCode);
                    if (lstLocationGoods[curIndex].QTY + 1 >= lstLocationGoods[curIndex].INVQTY)
                    {
                        lstLocationGoods[curIndex].QTY = lstLocationGoods[curIndex].INVQTY;
                        MessageBoxEx.Show("下架数量已是库存数量！");
                    }
                    else
                    {
                        lstLocationGoods[curIndex].QTY += 1;
                    }
                    txtLocation.Text = lstLocationGoods[curIndex].LOCATIONCODE;
                    txtQty.Text = lstLocationGoods[curIndex].QTY.ToString();
                    
                    //排序 
                    Sort(lstLocationGoods[curIndex]);
                    //设置已扫描的为加亮
                    SelectionBright();
                }
                else
                {
                    string barCode = gbCode.Substring(0, gbCode.Length - 8);
                    List<LocationGoodsInfo> checklgs = Duty.OffShelvesUIRule.NewInstance.CheckGetLocationGoodsInfo(gbCode);
                    if (checklgs != null)
                    {
                        foreach (LocationGoodsInfo lgItem in checklgs)
                        {
                            if (lstLocationGoods.FindIndex(x => x.GOODSBATCHCODE == lgItem.GOODSBATCHCODE) < 0)
                            {
                                string msg = string.Format("该商品存在更早批次库存,\n商品批次号:{0},\n名称:{1},库存:{2}{3}!\n是否重新扫描条码?",
                                    lgItem.GOODSBATCHCODE, lgItem.GOODSNAME, lgItem.INVQTY, lgItem.UNITNAME);
                                if (MessageBoxEx.Question(msg) == DialogResult.Yes)
                                {
                                    txtGoodsBatchCode.SelectAll();
                                    return;
                                }
                                //else if (lgItem.TIPNUMBER >= 2)
                                //{
                                //    if (MessageBoxEx.Question(string.Format("已多次不扫描该商品批次号:{0},是否设为无库存？", lgItem.GOODSBATCHCODE) == DialogResult.Yes))
                                //    {

                                //    }
                                //}
                            } 
                        }
                    }
 
                    LocationGoodsInfo _LocationGoods = Duty.OffShelvesUIRule.NewInstance.GetLocationGoodsInfo(gbCode);

                    if (_LocationGoods != null)
                    {
                        _LocationGoods.QTY = 0;
                        //_LocationGoods.GOODSBATCHCODE = gbCode;
                        //_LocationGoods.LOCATIONCODE = LtCode;
                        txtQty.Text = _LocationGoods.QTY.ToString();
                        lstLocationGoods.Insert(0, _LocationGoods);
                        arrayBarCode.Add(barCode);
                        //dictLocationGoods.Add(barCode, _LocationGoods);
                        //排序
                        Sort(_LocationGoods);
                    }
                    else
                    {
                        MessageBox.Show("没有找到匹配的数据！");
                        txtGoodsBatchCode.SelectAll();
                        return;
                    }
                }
                this.OffShelvesSource.DataSource = null;
                this.OffShelvesSource.DataSource = lstLocationGoods;
                this.dtGridOffShelves.DataSource = this.OffShelvesSource;
                this.dtGridOffShelves.Refresh();
                //设置已扫描的为加亮
                SelectionBright();
                txtQty.Focus();
                txtQty.SelectAll(); 
                  
            }
            catch(Exception ex)
            {
                txtGoodsBatchCode.SelectAll(); 
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex); 
            } 
            
        }
        //回车架位
        private void txtLocation_KeyDown(object sender, KeyEventArgs e) 
        {
            if (e.KeyCode != Keys.Enter) return;

            try
            {
                string LtCode = txtLocation.Text.Trim();
                if (string.IsNullOrEmpty(LtCode))
                {
                    MessageBox.Show("请扫架位条码！");
                    return;
                }
                string gbCode=txtGoodsBatchCode.Text.Trim();
                if(string.IsNullOrEmpty(gbCode))
                {
                    MessageBox.Show("商品批次条码为空！请先扫描商品批次条码");
                    return; 
                }
                LocationGoodsInfo lgItem=lstLocationGoods.Find(x=>x.GOODSBATCHCODE==gbCode);
                if (lgItem != null)
                {
                    if (LtCode != lgItem.LOCATIONCODE)
                    {
                        MessageBox.Show("扫描货位与系统记录货位不一致！");
                        return;
                    }
                }
                txtQty.Focus();
                txtQty.SelectAll(); 
 
            }
            catch (Exception ex)
            {
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex);

            } 

        }
        //回车数量
        private void txtQty_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;

            try
            {

                if (string.IsNullOrEmpty(txtQty.Text.Trim()))
                {
                    MessageBox.Show("请输入下架数量！");
                    return;
                }

                try
                {
                    decimal ofQty = Convert.ToDecimal(txtQty.Text); 
                }
                catch
                {
                    WoChu.CF.BaseFrame.Util.MessageBoxEx.Show("输入的数量不正确!");
                    this.txtQty.Text = string.Empty;
                    this.txtQty.Focus();
                    return;
                }
                decimal offQty = Convert.ToDecimal(txtQty.Text);
                if (offQty < 0)
                {
                    MessageBox.Show("下架数量不能小于零！");
                    return;
                }

                if (string.IsNullOrEmpty(txtGoodsBatchCode.Text) )
                {
                    MessageBox.Show("商品批次号不能为空！");
                    return;
                }
 
                string barCode = txtGoodsBatchCode.Text.Trim();
                string LtCode = txtLocation.Text.Trim();
                int curIndex = lstLocationGoods.FindIndex(x => x.GOODSBATCHCODE == barCode);
                if (curIndex >= 0)
                {
                    if (lstLocationGoods[curIndex].INVQTY < offQty)
                    {
                        //控制负库存
                        MessageBox.Show("下架数量大于当前库存数量,请更改下架数量！");
                        txtQty.SelectAll();
                        return;
                        //if (MessageBoxEx.Question("下架数量大于当前库存数量,是否更改下架数量？") == DialogResult.Yes)
                        //{
                        //    txtQty.SelectAll();
                        //    return;
                        //}
                        //else
                        //{
                        //    lstLocationGoods[curIndex].QTY = offQty;
                        //}

                    }
                    else
                    {
                        lstLocationGoods[curIndex].QTY = offQty;
                    }
                    //排序
                    Sort(lstLocationGoods[curIndex]);

                }
                else
                {
                    MessageBoxEx.Show("请先扫描正确的货位号和商品批次号！");
                    return;
                    //string _Code = barCode.Substring(0, 10);
                    //string locationCode = txtLocation.Text.Trim();
                    //LocationGoodsInfo _LocationGoods = Duty.OffShelvesUIRule.NewInstance.GetLocationGoodsInfo(_Code, locationCode);

                    //if (_LocationGoods != null)
                    //{
                    //    _LocationGoods.QTY = offQty;
                    //    _LocationGoods.GOODSBATCHCODE = barCode;
                    //    _LocationGoods.LOCATIONCODE = LtCode;
                    //    _LocationGoods.Selected = false;
                    //    lstLocationGoods.Insert(0, _LocationGoods);
                    //    //排序
                    //    Sort(_LocationGoods);
                    //}
                }

                this.OffShelvesSource.DataSource = null;
                this.OffShelvesSource.DataSource = lstLocationGoods;
                this.dtGridOffShelves.DataSource = this.OffShelvesSource;
                this.dtGridOffShelves.Refresh();
                //设置已扫描的为加亮
                SelectionBright();
                txtGoodsBatchCode.Text = string.Empty;
                txtLocation.Text = string.Empty;
                txtQty.Text = string.Empty;
                txtGoodsBatchCode.Focus(); 
            }
            catch (Exception ex)
            {
                txtQty.Focus();
                txtQty.SelectAll();
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex);

            }
            finally
            { 
            }
        }
        //排序
        private void Sort(LocationGoodsInfo locationGoods)
        {
            locationGoods.Sort = -1;
            lstLocationGoods.Sort((x, y) =>
            {
                return x.Sort.CompareTo(y.Sort);
            });
            this.dtGridOffShelves.Refresh(); 

            locationGoods.Sort = iSort++;
        }
        //显示行变色
        private void SelectionBright()
        {
            if (lstLocationGoods == null) return;
            if (lstLocationGoods.Count <= 0) return;
            //设置已扫描的为加亮
            if (string.IsNullOrEmpty(txtGoodsBatchCode.Text)) return; 
            string barCode = txtGoodsBatchCode.Text.Trim(); 
            int curIndex = lstLocationGoods.FindIndex(x => x.GOODSBATCHCODE == barCode);
            if (curIndex < 0) return;
            this.dtGridOffShelves.Select(curIndex);
            this.dtGridOffShelves.SelectionBackColor = Color.Green;
            
        }
        //关闭窗体操作
        private void FrmOffShelves_Closed(object sender, EventArgs e)
        {
           
            //System.Reflection.Assembly ass = System.Reflection.Assembly.Load("WoChu.RfClient.Login");
            //Type type = ass.GetType("WoChu.RfClient.Login.FrmMain");
            //Object o = Activator.CreateInstance(type);
            //System.Reflection.MethodInfo mi = type.GetMethod("Show");

            //mi.Invoke(o, null);
            lstLocationGoods.Clear();
            //dictLocation.Clear();
            //dictLocationGoods.Clear();
            goodsBox = null;
            this.Close();
            this.Dispose();
        }
        //关闭窗体提示
        private void FrmOffShelves_Closing(object sender, CancelEventArgs e)
        {
            if (lstLocationGoods != null )
            {
                if (lstLocationGoods.Count > 0)
                {
                    if (MessageBoxEx.Question("是否退出界面，将不保存界面数据？") == DialogResult.No)
                    {
                        e.Cancel = true;
                        return;
                    }
                }
            }
        }
        //删除操作
        private void dtGridOffShelves_DoubleClick(object sender, EventArgs e)
        {
            int curIndex = dtGridOffShelves.CurrentRowIndex;
            if (curIndex < 0) return;
            try
            {
                string gbCode = dtGridOffShelves[curIndex, 5].ToString();
                LocationGoodsInfo curItem = lstLocationGoods.Find(x => x.GOODSBATCHCODE == gbCode);
                string ltCode = curItem.LOCATIONCODE;
                string gbName = curItem.GOODSNAME;

                string Message = string.Format("货位:{0}, 名称:{1}, 商品批次号:{2} \n 是否删除该数据？", ltCode, gbName, gbCode);
                if (MessageBoxEx.Question(Message) == DialogResult.Yes)
                {
                    lstLocationGoods.Remove(curItem);

                    this.OffShelvesSource.DataSource = null;
                    this.OffShelvesSource.DataSource = lstLocationGoods;
                    this.dtGridOffShelves.DataSource = this.OffShelvesSource;
                    this.dtGridOffShelves.Refresh();
                }
            }
            catch (Exception ex)
            {
                TraceEx.Write(ex.Message);
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show("删除数据失败!");
            }
            finally
            {
                txtGoodsBatchCode.Focus(); 
                txtGoodsBatchCode.SelectAll();
            }
        }

        private void dtGridOffShelves_Click(object sender, EventArgs e)
        {
            int curIndex = dtGridOffShelves.CurrentRowIndex;
            if (curIndex < 0) return;

            string gbCode = dtGridOffShelves[curIndex, 5].ToString();
            LocationGoodsInfo curItem = lstLocationGoods.Find(x => x.GOODSBATCHCODE == gbCode);
            txtGoodsBatchCode.Text = gbCode;
            txtLocation.Text = curItem.LOCATIONCODE;
            txtQty.Text = curItem.QTY.ToString();
            txtQty.Focus();
            txtQty.SelectAll();
            SelectionBright();
        }
    }
}